package com.hcnow.websocket

import com.alibaba.fastjson.JSONObject
import com.hcnow.common.config.webSocket.WebSocketMessage
import org.slf4j.Logger
import org.slf4j.LoggerFactory
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.web.bind.annotation.*

@RestController
@RequestMapping("/api/webSocket")
class WebSocketApiController{
    private val logger: Logger = LoggerFactory.getLogger(this.javaClass)
    @Autowired
    lateinit var webSocketApiService: WebSocketApiService

    @PostMapping("/push/topic")
    fun pushTopicMessage(@RequestParam(value = "appId",required = true)appId:String,
                    @RequestParam(value = "projectId",required = true)projectId:String,
                    @RequestParam(value = "channel",required = true)channel:String,
                    @RequestBody message:JSONObject){
        logger.info("push.topic:$appId/$projectId/$channel")
        webSocketApiService.push2Topic(appId,projectId,channel, WebSocketMessage(message))
    }

    @PostMapping("/send/user")
    fun sendToUserMessage(@RequestParam(value = "appId",required = true)appId:String,
                          @RequestParam(value = "projectId",required = true)projectId:String,
                          @RequestParam(value = "userId",required = true)userId:String,
                          @RequestParam(value = "channel",required = true)channel:String,
                          @RequestBody message:JSONObject){
        logger.info("send.user:$appId.$projectId.$userId/$channel")
        webSocketApiService.send2User(appId,projectId,userId,channel, WebSocketMessage(message))
    }

}